//
//  HKDatePickerViewController.m
//  Pods
//
//  Created by 胡锦涛 on 2021/1/24.
//

#import "HKDatePickerViewController.h"
#import <HKLibFlexBox/YGLayout.h>
#import <HKLibFlexBox/UIView+Yoga.h>
#import <HKLibCommon/HKPrefixHeader.h>
#import <HKLibBase/HKLibBaseHeader.h>

/// 架构框架
#import <Bifrost/BifrostHeader.h>
#import <HKMediator/UIKitModuleService.h>

// 年月日选择器
#import <HKLibDatePicker/HKDatePicker.h>
// 年月选择器
#import <HKLibDatePicker/HKMonthPicker.h>
// 日历选择器
#import <HKLibDatePicker/HKCalendarView.h>

@interface HKDatePickerViewController ()<HKMonthPickerDelegate>
///  年月日按钮
@property (nonatomic, strong) QMUIButton * dayPickerButton ;
///  年月按钮
@property (nonatomic, strong) QMUIButton * monthPickerButton ;
///  日历按钮
@property (nonatomic, strong) QMUIButton * calendarPickerButton ;
/// 背景滚动视图
@property (nonatomic, strong) UIScrollView *bgScrollView ;
@end

@implementation HKDatePickerViewController
+ (void)load
{
    [Bifrost bindURL:kRouteDatePickerPage
           toHandler:^id _Nullable(NSDictionary * _Nullable parameters) {
        return [HKDatePickerViewController new];
    }];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = UIColorWhite;
    self.title = @"DatePicker日期选择";
    [self setupUI];
}

- (void)setupUI
{
    [self.view configureLayoutWithBlock:^(YGLayout * _Nonnull layout) {
        layout.isEnabled = YES;
        layout.alignItems = YGAlignStretch;
    }];
    self.bgScrollView = [[UIScrollView alloc] init] ;
    self.bgScrollView.backgroundColor = [UIColor grayColor];
    [self.bgScrollView configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled = YES;
        layout.flexDirection = YGFlexDirectionColumn;
        layout.width = YGPointValue(SCREEN_WIDTH);
        layout.height = YGPointValue(SCREEN_HEIGHT);
    }];
    [self.view addSubview:self.bgScrollView];
    // 年月日选择器
    [self createDayPickerView];
    // 年月选择器
    [self createMonthPickerView];
    // 日历选择器
    [self createCalendarPickerView];
    [self.bgScrollView.yoga applyLayoutPreservingOrigin:YES];
    self.bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view.yoga applyLayoutPreservingOrigin:YES];
}
/// 日历选择器
- (void)createCalendarPickerView
{
    UIView * flexContainer = [[UIView alloc] init];
    [flexContainer configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled     = YES;
        layout.flexDirection = YGFlexDirectionRow;
        layout.alignItems = YGAlignCenter;
        layout.paddingHorizontal = YGPointValue(5);
        layout.marginTop = layout.marginBottom = YGPointValue(20);
    }];
    YGLayoutConfigurationBlock layoutBlock = ^(YGLayout * layout){
        layout.isEnabled     = YES;
        layout.height= YGPointValue(60);
        layout.marginHorizontal = YGPointValue(5);
        layout.flexGrow = 1;
    };
    
    QMUIButton * calendarPickerButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [calendarPickerButton hk_setNormalTitle:@"请选择日历📅"];
    [calendarPickerButton hk_setNormalTitleColor:UIColorBlack];
    calendarPickerButton.backgroundColor = UIColorGreen;
    [calendarPickerButton configureLayoutWithBlock:layoutBlock];
    [calendarPickerButton hk_addTarget:self action:@selector(chooseCalendarPicker)];
    [flexContainer addSubview:calendarPickerButton];
    _calendarPickerButton = calendarPickerButton;
    
    [self.bgScrollView addSubview:flexContainer];
}
/// 年月选择器
- (void)createMonthPickerView
{
    UIView * flexContainer = [[UIView alloc] init];
    [flexContainer configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled     = YES;
        layout.flexDirection = YGFlexDirectionRow;
        layout.alignItems = YGAlignCenter;
        layout.paddingHorizontal = YGPointValue(5);
        layout.marginTop = layout.marginBottom = YGPointValue(20);
    }];
    YGLayoutConfigurationBlock layoutBlock = ^(YGLayout * layout){
        layout.isEnabled     = YES;
        layout.height= YGPointValue(60);
        layout.marginHorizontal = YGPointValue(5);
        layout.flexGrow = 1;
    };
    
    QMUIButton * dayPickerButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [dayPickerButton hk_setNormalTitle:@"请选择年月"];
    [dayPickerButton hk_setNormalTitleColor:UIColorBlack];
    dayPickerButton.backgroundColor = UIColorYellow;
    [dayPickerButton configureLayoutWithBlock:layoutBlock];
    [dayPickerButton hk_addTarget:self action:@selector(chooseMonthPicker)];
    [flexContainer addSubview:dayPickerButton];
    _monthPickerButton = dayPickerButton;
    
    [self.bgScrollView addSubview:flexContainer];
}
/// 年月日选择
- (void)createDayPickerView
{
    UIView * flexContainer = [[UIView alloc] init];
    [flexContainer configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled     = YES;
        layout.flexDirection = YGFlexDirectionRow;
        layout.alignItems = YGAlignCenter;
        layout.paddingHorizontal = YGPointValue(5);
        layout.marginTop = YGPointValue(50);
        layout.marginBottom = YGPointValue(20);
    }];
    YGLayoutConfigurationBlock layoutBlock = ^(YGLayout * layout){
        layout.isEnabled     = YES;
        layout.height= YGPointValue(60);
        layout.marginHorizontal = YGPointValue(5);
        layout.flexGrow = 1;
    };
    
    QMUIButton * dayPickerButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [dayPickerButton hk_setNormalTitle:@"请选择年月日"];
    [dayPickerButton hk_setNormalTitleColor:UIColorBlack];
    dayPickerButton.backgroundColor = UIColorRed;
    [dayPickerButton configureLayoutWithBlock:layoutBlock];
    [dayPickerButton hk_addTarget:self action:@selector(chooseDayPicker)];
    [flexContainer addSubview:dayPickerButton];
    _dayPickerButton = dayPickerButton;
    
    [self.bgScrollView addSubview:flexContainer];
}
#pragma mark -- ButtonAction --
/// 日历选择
- (void)chooseCalendarPicker
{
    HKCalendarView *calendarView  = [HKCalendarView initWithFrame:CGRectMake(0, HK_DEVICE_HEIGHT-400, HK_DEVICE_WIDTH, 400) andSelectedDate:  [NSDate date]];
    [calendarView show];
    
    HKWeakSelf
    [calendarView selectDate:^(NSString *selectDateString) {
        HKStrongSelf
        [calendarView hidden];
        [self.calendarPickerButton hk_setNormalTitle:selectDateString];
    }];
}
/// 年月选择
- (void)chooseMonthPicker
{
    HKMonthPicker * datePicker = [[HKMonthPicker alloc] initWithTitle:@"请选择年月" defaultDate:nil minDateStr:@"1900" maxDateStr:@"2025" isAutoSelect:NO];
    HKWeakSelf
    datePicker.onTap = ^(NSDateComponents *selectedComponents) {
        HKStrongSelf
        [HKKeyWindow hk_toast:HKStr(@"你选择的日期为：%ld%ld",selectedComponents.year,selectedComponents.month)];
        [self.monthPickerButton hk_setNormalTitle:HKStr(@"%ld-%ld",selectedComponents.year,selectedComponents.month)];
    };
    [datePicker showWithAnimation:YES];
}
/// 年月日选择
- (void)chooseDayPicker
{
    HKDatePicker * datePicker = [[HKDatePicker alloc] initWithTitle:@"请选择年月日" dateType:UIDatePickerModeDate defaultSelValue:nil minDateStr:@"1900-08-28 00:00:00" maxDateStr:@"2025-05-05 00:00:00" isAutoSelect:NO];
    HKWeakSelf
    datePicker.onTap = ^(NSDateComponents *dateComponnets)
    {
        HKStrongSelf
        [HKKeyWindow hk_toast:HKStr(@"你选择的日期为：%ld-%ld-%ld",dateComponnets.year,dateComponnets.month,dateComponnets.day)];
        [self.dayPickerButton hk_setNormalTitle:HKStr(@"%ld-%ld-%ld",dateComponnets.year,dateComponnets.month,dateComponnets.day)];
    };
    [datePicker showWithAnimation:YES];
}
@end
